import React, { Component } from 'react';
import {
	StyleSheet, Text, View, Image, ScrollView, TouchableOpacity
} from "react-native";
import { GLOBAL } from '../../../../config/global';
import { style } from '../../../../theme/style';
import { Common } from '../../../../component/common/Common';
import { orderStyle, commonStyle, commen } from '../_common/style';
import navigationSeevice from '../../../../components/navigation/navigationSeevice'
import { PageStatusIndex } from '../../../../component/page-status/PageStatusIndex';
import OrderDetailsService from '../_common/service';
import { TopShowCpt } from '../_components/TopShowCpt';
import { CURRENCY } from '../../../../utils/helper';
import MethodUtil from '../../../../utils/MethodUtil';
const scale = GLOBAL.SCALE;


export class OrderAccountComponent extends Component {
	static navigationOptions = ({ navigation }) => { };
	constructor(props) {
		super(props);
		console.log('发票页面参数', props)
		let { orderType, orderId, tCode } = this.props.data || {};
		this.propsSend = { orderType, orderId, tCode };
		this.state = {
			status: 'loading',
			data: null,
		}
		// this.endDataTiem = '';
		// this.endDataTiem = '';
	}

	async	componentWillMount () {
		this._init();
	}

	//初始化
	_init = () => {
		this._getPoolData();
		this._itemTime();
	}

	// 获取数据汇总
	_getPoolData = async () => {
		let responseData = await OrderDetailsService.fpPool({ orderId: this.propsSend.orderId });
		console.log('发票汇总数据', responseData)
		if (!responseData) return;
		this.setState({ poolData: responseData })
	}
	_itemTime = async () => {
		this.startTime = this.GetDateStr(-6);//前六个月
		this.endDataTiem = this.GetDateStr(0);//当前月
	}

	// 时间方法
	GetDateStr = (AddDayCount) => {
		var dd = new Date();
		var y = dd.getFullYear();
		var item = dd.getDate();
		let m;
		let month = dd.getMonth() + 1;
		if ((month + AddDayCount) <= 12) {
			if (month + AddDayCount > 0) {
				m = (AddDayCount + month)
				if (m < 10) {
					m = (AddDayCount + month)
				}
			} else {
				m = (month + AddDayCount + 12);
				y = y - 1
				if (m < 10) {
					m = (month + AddDayCount - 12)
				}
			}
		} else {
			y = y + 1;
			m = (month + AddDayCount - 12)
			if (m < 10) {
				m = (month + AddDayCount - 12)
			}
		}
		return y + "-" + m + "-" + item;
	}
	goSkipPage = (poolData) => {
		navigationSeevice.navigate('BillListPage', {
			companyName: this.props.orderData.companyName || '',
			tabIndex: this.props.orderData.orderType == 'S' ? 2 : 1,
			strtime: this.startTime,
			endTime: this.endDataTiem,
		})

	}

	render () {
		console.log('发票-page')
		let { data, poolData } = this.state;
		let { storageAmount, settlementAmount, unInvoiceAmount, currency } = poolData || {};
		return (
			<View style={orderStyle.container}>
				<View style={orderStyle.scollBox}>
					<View style={orderStyle.showBox}>
						<TopShowCpt data={{
							imgData: { big: storageAmount, small: settlementAmount },
							listData: [
								{ title: `${this.propsSend.orderType == 'S' ? '应开' : '应收'}发票`, value: `${storageAmount == null ? '-' : MethodUtil.toThousands(storageAmount)}${storageAmount == null ? '' : CURRENCY.getUnit(currency).text}` },
								{ title: `${this.propsSend.orderType == 'S' ? '实开' : '实收'}发票`, value: `${settlementAmount == null ? '-' : MethodUtil.toThousands(settlementAmount)}${settlementAmount == null ? '' : CURRENCY.getUnit(currency).text}` },
								{ title: `待${this.propsSend.orderType == 'S' ? '实开' : '实收'}`, value: `${unInvoiceAmount == null ? '-' : MethodUtil.toThousands(unInvoiceAmount)}${unInvoiceAmount == null ? '' : CURRENCY.getUnit(currency).text}`, color: true }
							]
						}} />
					</View>

					<View style={{ flex: 1 }}>
						{data ? (
							<View style={orderStyle.body}>
							</View>
						) : <View style={styles.infoBox}>
								{/* <Text style={styles.info}>发票明细敬请期待...</Text> */}
								<TouchableOpacity onPress={() => { this.goSkipPage(poolData) }}>
									<Text style={styles.itemInfo}>点击查看发票</Text>
								</TouchableOpacity>
							</View>}
					</View>



				</View>
			</View >
		)
	}
}


const styles = StyleSheet.create({
	infoBox: {
		flex: 1,
		flexDirection: 'row',
		justifyContent: 'center',
		alignItems: 'center'
	},
	info: {
		fontSize: 16 * scale,
		color: '#666'
	},
	itemInfo: {
		fontSize: 16 * scale,
		color: '#0E7FFD'
	},
})